package managedbean.itinerary;

import entity.DailyItineraryEntity;
import exception.ExistException;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Time;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.ItinerarySessionBean;
import session.stateless.SystemMessagesBean;
import util.entityhelper.DailyItineraryState;
import util.entityhelper.TripItineraryState;
import util.generator.TouristMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class ItineraryViewTripItineraryManagedBean implements Serializable {

    @EJB
    private ItinerarySessionBean itineraryMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    private PageRedirectorBean redirect;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private TripItineraryState tripItineraryState;
    
    // fields passed from session
    private Long touristAccountId;
    private String preferredLocale;
    private String statusMessageFromEdit;
    private String itineraryType;
    private Long selectedTripItineraryId;
    private Long selectedDailyItineraryId;
    
    // other fields from database (+ display)
    private String itineraryName;
    private String arrivalDate;
    private Time arrivalTime;
    private String arrivalTimeZone;
    private String departureDate;
    private Time departureTime;
    private String departureTimeZone;
    private String accomodationPostalCode;
    private Integer noOfAdults;
    private Integer noOfChildren;
    private Collection<DailyItineraryState> dailyItinerariesList;
    
    // display only
    private MenuModel touristMainMenuModel;
    private MenuModel touristNavMenuModel;
    
    public ItineraryViewTripItineraryManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        redirect = new PageRedirectorBean();
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("touristAccountId")) {
            touristAccountId = (Long) ctx.getExternalContext().getSessionMap().get("touristAccountId");
            // put back the session IDs taken
            ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountId);
            
            if (ctx.getExternalContext().getSessionMap().containsKey("statusMessageFromEdit")) {
                statusMessageFromEdit = (String) ctx.getExternalContext().getSessionMap().get("statusMessageFromEdit");
            }
            
            if (ctx.getExternalContext().getSessionMap().containsKey("itineraryType")) {
                itineraryType = (String) ctx.getExternalContext().getSessionMap().get("itineraryType");
                ctx.getExternalContext().getSessionMap().put("itineraryType", itineraryType);
            }
            
            if (ctx.getExternalContext().getSessionMap().containsKey("selectedTripItineraryId")) {
                selectedTripItineraryId = (Long) ctx.getExternalContext().getSessionMap().get("selectedTripItineraryId");
                ctx.getExternalContext().getSessionMap().put("selectedTripItineraryId", selectedTripItineraryId);
            }
            
            if (ctx.getExternalContext().getSessionMap().containsKey("selectedDailyItineraryId")) {
                selectedDailyItineraryId = (Long) ctx.getExternalContext().getSessionMap().get("selectedDailyItineraryId");
                ctx.getExternalContext().getSessionMap().put("selectedDailyItineraryId", selectedDailyItineraryId);
            }
            
        }
        else {
            // redirect back to login
            redirect.goTouristLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */    
    }
    
    public void initView(PhaseEvent event) {
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        try {
            if (statusMessageFromEdit != null) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessageFromEdit, ""));
            }
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            itineraryMgr.setSelectedLocale(preferredLocale);
            // get data from backend, exception alrd thrown by backend if these methods fail.
            
            try {
                tripItineraryState = itineraryMgr.getTripItineraryByIDReturnByTripItineraryState(selectedTripItineraryId);
            }
            catch (ExistException ex) {
//                System.out.println(ex.getMessage());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
            }
            
            itineraryName = tripItineraryState.getItineraryName();
            
            arrivalDate = localization.getLocalizedDateFormat(tripItineraryState.getArrivalDate());
            
            arrivalTime = tripItineraryState.getArrivalTime();
            
            arrivalTimeZone = localization.getLocalizedTimeZone(tripItineraryState.getArrivalTimeZone());
            
            departureDate = localization.getLocalizedDateFormat(tripItineraryState.getDepartureDate());
            
            departureTime = tripItineraryState.getDepartureTime();
            
            departureTimeZone = localization.getLocalizedTimeZone(tripItineraryState.getDepartureTimeZone());
            
            accomodationPostalCode = tripItineraryState.getAccomodationPostalCode();
            
            noOfAdults = tripItineraryState.getNoOfAdults();
            
            noOfChildren = tripItineraryState.getNoOfChildren();
            
            try {
                dailyItinerariesList = itineraryMgr.getDailyItinerariesByTripItineraryIDReturnByDailyItineraryState(selectedTripItineraryId);
                for (DailyItineraryState d : dailyItinerariesList)
                {
                    d.setLocalizedDayDate(localization.getLocalizedDateFormat(d.getDayDate()));
                }
            }
            catch (ExistException ex) {
//                System.out.println(ex.getMessage());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
            }
            
            // main menu 
            TouristMenuGenerator menuGeneratorMain = new TouristMenuGenerator();
            touristMainMenuModel = menuGeneratorMain.generateTouristMainMenu();
            // nav menu
            TouristMenuGenerator menuGeneratorNav = new TouristMenuGenerator();
            touristNavMenuModel = menuGeneratorNav.generateTouristNavMenu();
        }
        catch (Exception ex) {
            ex.printStackTrace();
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }
    
    public void viewDailyItineraryView(DailyItineraryState dailyItinerary) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedDailyItineraryId", dailyItinerary.getId());
        System.out.println("selectedDailyItinerary: " + FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedDailyItineraryId"));
//        redirect.goItineraryTripItineraryPage();
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/itinerary/ItineraryViewDailyItinerary.xhtml"));
        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
        
        //TODO: edit daily page
    }

    public void viewDailyItineraryDelete(DailyItineraryState dailyItinerary) throws IOException {
        //TODO: delete daily page
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedDailyItineraryId", dailyItinerary.getId());
//        redirect.goDeleteTripItineraryPage();
    }

    public String getAccomodationPostalCode() {
        return accomodationPostalCode;
    }

    public void setAccomodationPostalCode(String accomodationPostalCode) {
        this.accomodationPostalCode = accomodationPostalCode;
    }

    public String getArrivalDate() {
        return arrivalDate;
    }

    public void setArrivalDate(String arrivalDate) {
        this.arrivalDate = arrivalDate;
    }

    public Time getArrivalTime() {
        return arrivalTime;
    }

    public void setArrivalTime(Time arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }
    
    public String getDepartureDate() {
        return departureDate;
    }

    public void setDepartureDate(String departureDate) {
        this.departureDate = departureDate;
    }

    public Time getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Time departureTime) {
        this.departureTime = departureTime;
    }

    public ItinerarySessionBean getItineraryMgr() {
        return itineraryMgr;
    }

    public void setItineraryMgr(ItinerarySessionBean itineraryMgr) {
        this.itineraryMgr = itineraryMgr;
    }

    public String getItineraryName() {
        return itineraryName;
    }

    public void setItineraryName(String itineraryName) {
        this.itineraryName = itineraryName;
    }

    public String getItineraryType() {
        return itineraryType;
    }

    public void setItineraryType(String itineraryType) {
        this.itineraryType = itineraryType;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public Integer getNoOfAdults() {
        return noOfAdults;
    }

    public void setNoOfAdults(Integer noOfAdults) {
        this.noOfAdults = noOfAdults;
    }

    public Integer getNoOfChildren() {
        return noOfChildren;
    }

    public void setNoOfChildren(Integer noOfChildren) {
        this.noOfChildren = noOfChildren;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public PageRedirectorBean getRedirect() {
        return redirect;
    }

    public void setRedirect(PageRedirectorBean redirect) {
        this.redirect = redirect;
    }

    public String getStatusMessageFromEdit() {
        return statusMessageFromEdit;
    }

    public void setStatusMessageFromEdit(String statusMessageFromEdit) {
        this.statusMessageFromEdit = statusMessageFromEdit;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public Long getTouristAccountId() {
        return touristAccountId;
    }

    public void setTouristAccountId(Long touristAccountId) {
        this.touristAccountId = touristAccountId;
    }

    public MenuModel getTouristMainMenuModel() {
        return touristMainMenuModel;
    }

    public void setTouristMainMenuModel(MenuModel touristMainMenuModel) {
        this.touristMainMenuModel = touristMainMenuModel;
    }

    public MenuModel getTouristNavMenuModel() {
        return touristNavMenuModel;
    }

    public void setTouristNavMenuModel(MenuModel touristNavMenuModel) {
        this.touristNavMenuModel = touristNavMenuModel;
    }

    public TripItineraryState getTripItineraryState() {
        return tripItineraryState;
    }

    public void setTripItineraryState(TripItineraryState tripItineraryState) {
        this.tripItineraryState = tripItineraryState;
    }

    public String getArrivalTimeZone() {
        return arrivalTimeZone;
    }

    public void setArrivalTimeZone(String arrivalTimeZone) {
        this.arrivalTimeZone = arrivalTimeZone;
    }

    public String getDepartureTimeZone() {
        return departureTimeZone;
    }

    public void setDepartureTimeZone(String departureTimeZone) {
        this.departureTimeZone = departureTimeZone;
    }

    public Collection<DailyItineraryState> getDailyItinerariesList() {
        return dailyItinerariesList;
    }

    public void setDailyItinerariesList(Collection<DailyItineraryState> dailyItinerariesList) {
        this.dailyItinerariesList = dailyItinerariesList;
    }

    public Long getSelectedDailyItineraryId() {
        return selectedDailyItineraryId;
    }

    public void setSelectedDailyItineraryId(Long selectedDailyItineraryId) {
        this.selectedDailyItineraryId = selectedDailyItineraryId;
    }

    public Long getSelectedTripItineraryId() {
        return selectedTripItineraryId;
    }

    public void setSelectedTripItineraryId(Long selectedTripItineraryId) {
        this.selectedTripItineraryId = selectedTripItineraryId;
    }
}
